/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "arm_math.h"
#include "uart_print.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
static void DSP_CONJ(void)
{
  uint8_t i;
  float32_t f32Src[10]={
      0.1, 0.11,
      0.2, 0.22,
      0.3, 0.33,
      0.4, 0.44,
      0.5, 0.55,
  };
  float32_t f32Dst[10];
  q31_t q31Src[10], q31Dst[10];
  q15_t q15Src[10], q15Dst[10];

  arm_cmplx_conj_f32(f32Src, f32Dst, 5);
  uart_print("*********************************************\n");
  uart_print("cal f32 type conjugate\n");
  for(i=0; i<5; i++)
  {
    uart_print("array %d. real %f, image: %f.\n", i, f32Dst[2*i], f32Dst[2*i+1]);
  }

  uart_print("*********************************************\n");
  uart_print("cal Q31 type conjugate\n");
  arm_float_to_q31(f32Src, q31Src, 10);
  arm_cmplx_conj_q31(q31Src, q31Dst, 5);
  arm_q31_to_float(q31Dst, f32Dst, 10);
  for(i=0; i<5; i++)
  {
    uart_print("Q31 array %d。Input, real: 0x%X, image: 0x%X. Output, real: 0x%X, image: 0x%X, float, real: %f, image: %f.\n",
        i,
        q31Src[2*i], q31Src[2*i+1],
        q31Dst[2*i], q31Dst[2*i+1],
        f32Dst[2*i], f32Dst[2*i+1]);
  }

  uart_print("*********************************************\n");
  uart_print("cal Q15 type conjugate\n");
  arm_float_to_q15(f32Src, q15Src, 10);
  arm_cmplx_conj_q15(q15Src, q15Dst, 5);
  arm_q15_to_float(q15Dst, f32Dst, 10);
  for(i=0; i<5; i++)
  {
    uart_print("Q15 array %d。Input, real: 0x%X, image: 0x%X. Output, real: 0x%X, image: 0x%X, float, real: %f, image: %f.\n",
        i,
        q15Src[2*i], q15Src[2*i+1],
        q15Dst[2*i], q15Dst[2*i+1],
        f32Dst[2*i], f32Dst[2*i+1]);
  }
}

static void DSP_CmplxDotProd(void)
{
  float32_t f32SrcA[4] = {0.1, 0.11, 0.2, 0.21};
  float32_t f32SrcB[4] = {0.1, 0.11, 0.2, 0.21};
  float32_t f32realRes, f32imagRes;

  q31_t q31SrcA[4], q31SrcB[4];
  q63_t q63realRes, q63imagRes;

  q15_t q15SrcA[4], q15SrcB[4];
  q31_t q31realRes, q31imagRes;
  q15_t q15realRes, q15imagRes;

  arm_cmplx_dot_prod_f32(f32SrcA, f32SrcB, 2, &f32realRes, &f32imagRes);
  uart_print("arm_cmplx_dot_prod_f32, real: %f, image: %f.\n", f32realRes, f32imagRes);

  arm_float_to_q31(f32SrcA, q31SrcA, 4);
  arm_float_to_q31(f32SrcB, q31SrcB, 4);
  arm_cmplx_dot_prod_q31(q31SrcA, q31SrcB, 2, &q63realRes, &q63imagRes);

  uart_print("arm_cmplx_dot_prod_q31, real: %lld, image: %lld.\n", q63realRes, q63imagRes);

  arm_float_to_q15(f32SrcA, q15SrcA, 4);
  arm_float_to_q15(f32SrcB, q15SrcB, 4);
  arm_cmplx_dot_prod_q15(q15SrcA, q15SrcB, 2, &q31realRes, &q31imagRes);

  uart_print("arm_cmplx_dot_prod_q15, real: %d, image: %d.\n", q31realRes, q31imagRes);
}

static void DSP_CmplxMag(void)
{
  float f32Src[10]={0.1, 0.1, 0.21, 0.21, 0.31, 0.31, 0.41, 0.41, 0.51, 0.51};
  float f32Dst[10];
  q31_t q31Src[10], q31Dst[5];
  q15_t q15Src[10], q15Dst[5];

  uint8_t i;

  arm_cmplx_mag_f32(f32Src, f32Dst, 5);
  for(i=0; i<5; i++)
  {
    uart_print("f32 %d mag, %f.\n", i, f32Dst[i]);
  }

  arm_float_to_q31(f32Src, q31Src, 10);
  arm_cmplx_mag_q31(q31Src, q31Dst, 5);
  for(i=0; i<5; i++)
  {
    uart_print("q31 %d mag, %d.\n", i, q31Dst[i]);
  }

  arm_float_to_q15(f32Src, q15Src, 10);
  arm_cmplx_mag_q15(q15Src, q15Dst, 5);
  for(i=0; i<5; i++)
  {
    uart_print("q15 %d mag, %d.\n", i, q15Dst[i]);
  }
}
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */
/* Enable the CPU Cache */

  /* Enable I-Cache---------------------------------------------------------*/
  SCB_EnableICache();

  /* Enable D-Cache---------------------------------------------------------*/
  SCB_EnableDCache();

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
//  DSP_CONJ();
//  DSP_CmplxDotProd();
  DSP_CmplxMag();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 432;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Activate the Over-Drive mode
  */
  if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
